<template>
  <view class="container">
    <view>
      <block
          v-for="(item, index) in prodList"
          :key="index"
      >
        <production :item="item"/>
      </block>
      <view
          v-if="!prodList.length"
          class="empty"
      >
        暂无数据
      </view>
    </view>
  </view>
</template>

<script >
import {goods} from "../../api/index/prodClassify";
import {mineCollectionProd} from "../../api/mine.js"
export default {
  data() {
    return {
      sts: 0,
      title: '',
      current: 1,
      size: 10,
      pages: 0,
      tagid: 0,
      prodList: []
    }
  },
  methods: {
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      if (this.current < this.pages) {
        this.current = this.current + 1
        this.loadProdData()
      }
    },
    /**
     * 加载商品数据
     */
    loadProdData(options) {
      const stsParam = this.sts
      if (stsParam == 0) {
        // 分组标签商品列表
        this.getTagProd()
      } else if (stsParam == 1) {
        // 新品推荐
        const url = '/prod/lastedProdPage'
        this.getActProd(url)
      } else if (stsParam == 2) {
        // 限时特惠
        const url = '/prod/discountProdList'
        this.getActProd(url)
      } else if (stsParam == 3) {
        // 每日疯抢
        const url = '/prod/moreBuyProdList'
        this.getActProd(url)
      } else if (stsParam == 4) {
        // 优惠券商品列表
        this.getProdByCouponId(options.tagid)
      } else if (stsParam == 5) {
        // 收藏商品列表
        this.getCollectionProd()
      }
    },
    getActProd(url) {
      uni.showLoading()
      goods(this.sts)
     .then(({rows}) => {
             const activeItems = rows.filter(item => item.status ==0);
             console.log(activeItems)
             rows.forEach(r=>{
               const pic=r.pic;
               const num=pic.split(",");
               r.pic=num[0]
             })
             console.log(rows);
             this.prodList = activeItems
        uni.hideLoading()
      })
      /* http.request({
         url,
         method: 'GET',
         data: {
           current: this.current,
           size: this.size
         }
       })
           .then(({ data }) => {
             let list
             if (data.current === 1) {
               list = data.records
             } else {
               list = this.prodList
               list = list.concat(data.records)
             }
             this.prodList= list
             this.pages= data.pages
             uni.hideLoading()
           })*/
    },
    /**
     * 获取我的收藏商品
     */
    getCollectionProd() {
      uni.showLoading()
      mineCollectionProd()
	  .then(({data}) => {
        this.prodList = data
      console.log(data)
		this.pages = data.length
        uni.hideLoading()
      })
      /*http.request({
        url: '/p/user/collection/prods',
        method: 'GET',
        data: {
          current: this.current,
          size: this.size
        }
      })
          .then(({ data }) => {
            let list
            if (data.current == 1) {
              list = data.records
            } else {
              list = this.prodList
              list = list.concat(data.records)
            }
            this.prodList= list
            this.pages= data.pages
            uni.hideLoading()
          })*/
    },
    /**
     * 获取标签列表
     */
    getTagProd() {
      uni.showLoading()
      /*request("/prod/prodListByTagId", "GET", {
        tagId: this.tagid,
        current: this.current,
        size: this.size
      })*/
         goods(this.tagid)
          .then(({rows})=> {
            const activeItems = rows.filter(item => item.status ==0);
            console.log(activeItems)
            rows.forEach(r=>{
             const pic=r.pic;
              const num=pic.split(",");
              r.pic=num[0]
            })
            console.log(rows);
        this.prodList = activeItems
        uni.hideLoading()
      })
      /*http.request({
        url: '/prod/prodListByTagId',
        method: 'GET',
        data: {
          tagId: this.tagid,
          current: this.current,
          size: this.size
        }
      })
          .then(({ data }) => {
            let list
            if (data.current === 1) {
              list = data.records
            } else {
              list = this.prodList.concat(data.records)
            }
            this.prodList= list
            this.pages= data.pages
            uni.hideLoading()
          })*/
    },
    /**
     * 获取优惠券商品列表
     */
    getProdByCouponId(id) {
      uni.showLoading()
      request("/coupon/prodListByCouponId", "GET", {
        tagId: this.tagid,
        current: this.current,
        size: this.size
      }).then(({data}) => {
        let list
        if (data.current === 1) {
          list = data.records
        } else {
          list = this.prodList.concat(data.records)
        }
        this.prodList = list
        this.pages = data.pages
        uni.hideLoading()
      })
      /*http.request({
        url: '/coupon/prodListByCouponId',
        method: 'GET',
        data: {
          couponId: id,
          current: this.current,
          size: this.size
        }
      })
          .then(({ data }) => {
            let list
            if (data.current === 1) {
              list = data.records
            } else {
              list = this.prodList.concat(data.records)
            }
            this.prodList= list
            this.pages= data.pages
            uni.hideLoading()
          })*/
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.current = 1
    this.pages = 0
    this.sts = options.sts
    this.title = options.title ? options.title : ''

    if (options.tagid) {
      this.tagid = options.tagid
    }

    if (this.sts == 0) {
      if (options.tagid == 1) {
        uni.setNavigationBarTitle({
          title: '每日上新'
        })
      } else if (options.tagid == 2) {
        uni.setNavigationBarTitle({
          title: '商城热卖'
        })
      } else if (options.tagid == 3) {
        uni.setNavigationBarTitle({
          title: '更多宝贝'
        })
      }
    } else if (this.sts == 1) {
      uni.setNavigationBarTitle({
        title: '新品推荐'
      })
    } else if (this.sts == 2) {
      uni.setNavigationBarTitle({
        title: '限时特惠'
      })
    } else if (this.sts == 3) {
      uni.setNavigationBarTitle({
        title: '每日疯抢'
      })
    } else if (this.sts == 4) {
      uni.setNavigationBarTitle({
        title: '优惠券活动商品'
      })
    } else if (this.sts == 5) {
      uni.setNavigationBarTitle({
        title: '我的收藏商品'
      })
    } else {
      uni.setNavigationBarTitle({
        title: this.title
      })
    }
    this.loadProdData(options)
  }
}

</script>

<style scoped lang="scss">
.container {
  background: #fff;
  margin: 7px;
}
.line-fix {
  width: 100%;
  height: 2rpx;
  background: #e1e1e1;
  position: fixed;
  top: 0;
}
.tit-background {
  width: 100%;
  height: 20rpx;
  background: #f4f4f4;
}
.prod-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
/* 空 */
.empty {
  display: block;
  width: 100%;
  font-size: 26rpx;
  color: #999;
  margin-top: 20vh;
  text-align: center;
}

</style>
